package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.annotation.LegalValidate;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.entity.SysDept;
import com.cz.video.common.core.domain.entity.SysUser;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.helper.DataBaseHelper;
import com.cz.video.common.utils.StreamUtils;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteAccount;
import com.cz.video.website.domain.bo.WebsiteAccountBo;
import com.cz.video.website.mapper.WebsiteAccountMapper;
import com.cz.video.website.service.IWebsiteAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户账户表 服务实现类
 * </p>
 *
 * @author 游蓉
 * @since 2023-10-31
 */
@Service
@RequiredArgsConstructor
public class WebsiteAccountServiceImpl extends ServiceImpl<WebsiteAccountMapper, WebsiteAccount> implements IWebsiteAccountService {

    private final WebsiteAccountMapper accountMapper;
    /**
     * 校验用户名称是否唯一
     *
     * @param websiteAccount 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(WebsiteAccountBo websiteAccount) {
        boolean exist = accountMapper.exists(new LambdaQueryWrapper<WebsiteAccount>()
            .eq(WebsiteAccount::getUsername, websiteAccount.getUsername())
            .ne(ObjectUtil.isNotNull(websiteAccount.getAccountId()), WebsiteAccount::getAccountId, websiteAccount.getAccountId()));
        return !exist;
    }
    /**
     * 新增保存用户信息
     *
     * @param websiteAccount 用户信息
     * @return 结果
     */
    @Override
    @LegalValidate
    @Transactional(rollbackFor = Exception.class)
    public int insertUser(WebsiteAccountBo websiteAccount) {
        // 新增用户信息
        int rows = accountMapper.insert(websiteAccount);
        return rows;
    }

    /**
     * 修改用户信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(WebsiteAccountBo websiteAccount) {
        Long userId =websiteAccount.getAccountId();
        return accountMapper.updateById(websiteAccount);
    }
    /**
     * 通过角色ID查询角色
     *
     * @param id 角色ID
     * @return 角色对象信息
     */
    @Override
    public WebsiteAccount selectUserById(Long id) {
        return accountMapper.selectById(id);
    }

    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public TableDataInfo<WebsiteAccount> selectPageUserList(WebsiteAccountBo user, PageQuery pageQuery) {
        Page<WebsiteAccount> page = accountMapper.selectPageUserList(pageQuery.build(), this.buildQueryWrapper(user));
        return TableDataInfo.build(page);
    }

    private Wrapper<WebsiteAccount> buildQueryWrapper(WebsiteAccountBo user) {
        Map<String, Object> params = user.getParams();
        QueryWrapper<WebsiteAccount> wrapper = Wrappers.query();

        wrapper.eq("u.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(user.getAccountId()), "u.user_id", user.getAccountId())
            .like(StringUtils.isNotBlank(user.getUsername()), "u.user_name", user.getUsername())
            .eq(StringUtils.isNotBlank(user.getStatus()), "u.status", user.getStatus())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "u.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
